package com.edu.hre.struts.action.admin;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.edu.hre.common.utils.BeanFinder;
import com.edu.hre.common.utils.ComboBoxInit;
import com.edu.hre.common.utils.CommonUtils;
import com.edu.hre.common.utils.StringConstants;
import com.edu.hre.db.pojo.HreLocation;
import com.edu.hre.db.service.BaseService;
import com.edu.hre.struts.form.admin.LocationUpdateForm;

public class LocationUpdateAction extends DispatchAction {

	private static final Logger logger = Logger.getLogger(LocationUpdateAction.class);
	private static final BaseService baseService = BeanFinder.getBean(BaseService.BEAN_NAME, BaseService.class);
	private final String ACTION_NAME = "locationUpdate";
	
	public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		CommonUtils.println(CommonUtils.getSourceCodeLocation());
		LocationUpdateForm beanForm = (LocationUpdateForm) form;
		beanForm.setIsAddNew(true);

		if(request.getParameter(StringConstants.BTN_ADD) == null){
			beanForm.setMessage(StringConstants.BLANK);
			initFormBean(beanForm);
			return mapping.findForward(ACTION_NAME);
		}

		String message = validateForm(beanForm);
		if(!StringUtils.isBlank(message)){
			beanForm.setMessage(message);
			initFormBean(beanForm);
			return mapping.findForward(ACTION_NAME);
		}
		
		// Save or update.
		HreLocation tmp = new HreLocation();
		tmp.setName(CommonUtils.normalizeString(beanForm.getName()));
		tmp.setProvince(CommonUtils.normalizeString(beanForm.getProvince()));
		tmp.setDistrict(CommonUtils.normalizeString(beanForm.getDistrict()));
		tmp.setAddress(CommonUtils.normalizeString(beanForm.getAddress()));
		tmp.setPhone(CommonUtils.normalizeString(beanForm.getPhone()));
		tmp.setFax(CommonUtils.normalizeString(beanForm.getFax()));
		tmp.setNotes(CommonUtils.normalizeString(beanForm.getNotes()));
		tmp.setDateCreated(new Date());
		tmp.setDateModified(new Date());
		
		boolean status = baseService.saveOrUpdate(tmp);
		if (!status) {
			beanForm.setMessage(StringConstants.INSERT_FAILURE_MESSAGE);
			initFormBean(beanForm);
			return mapping.findForward(ACTION_NAME);
		}

		response.getWriter().write(StringConstants.POP_UP_CLOSE_SCRIPT);
		return null;
	}
	
	public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		CommonUtils.println(CommonUtils.getSourceCodeLocation());
		LocationUpdateForm beanForm = (LocationUpdateForm) form;
		beanForm.setIsAddNew(false); 
		
		if(request.getParameter(StringConstants.BTN_EDIT) == null){

			int id = CommonUtils.getIntParameter("id", request);
			HreLocation tmp = baseService.getPojo(id, HreLocation.class);
			
			if (tmp == null) {
				beanForm.setMessage(StringConstants.BLANK);
				return mapping.findForward(ACTION_NAME);
			}
			
			beanForm.setId(id);
			beanForm.setName(CommonUtils.normalizeString(tmp.getName()));
			beanForm.setProvince(CommonUtils.normalizeString(tmp.getProvince()));
			beanForm.setDistrict(CommonUtils.normalizeString(tmp.getDistrict()));
			beanForm.setAddress(CommonUtils.normalizeString(tmp.getAddress()));
			beanForm.setPhone(CommonUtils.normalizeString(tmp.getPhone()));
			beanForm.setFax(CommonUtils.normalizeString(tmp.getFax()));
			beanForm.setNotes(CommonUtils.normalizeString(tmp.getNotes()));

			initFormBean(beanForm);
			beanForm.setMessage(StringConstants.BLANK);
			return mapping.findForward(ACTION_NAME);
		}
		
		String message = validateForm(beanForm);
		if(!StringUtils.isBlank(message)){
			beanForm.setMessage(message);
			initFormBean(beanForm);
			return mapping.findForward(ACTION_NAME);
		}

		HreLocation tmp = baseService.getPojo(beanForm.getId(), HreLocation.class);
		if (tmp == null) {
			beanForm.setMessage(StringConstants.BLANK);
			return mapping.findForward(ACTION_NAME);
		}

		tmp.setId(beanForm.getId());
		tmp.setName(CommonUtils.normalizeString(beanForm.getName()));
		tmp.setProvince(CommonUtils.normalizeString(beanForm.getProvince()));
		tmp.setDistrict(CommonUtils.normalizeString(beanForm.getDistrict()));
		tmp.setAddress(CommonUtils.normalizeString(beanForm.getAddress()));
		tmp.setPhone(CommonUtils.normalizeString(beanForm.getPhone()));
		tmp.setFax(CommonUtils.normalizeString(beanForm.getFax()));
		tmp.setNotes(CommonUtils.normalizeString(beanForm.getNotes()));
		tmp.setDateModified(new Date());
		
		boolean status = baseService.saveOrUpdate(tmp);
		if (!status) {
			beanForm.setMessage(StringConstants.UPDATE_FAILURE_MESSAGE);
			initFormBean(beanForm);
			return mapping.findForward(ACTION_NAME);
		}

		response.getWriter().write(StringConstants.POP_UP_CLOSE_SCRIPT);
		return null;
	}
	
	private void initFormBean(LocationUpdateForm beanForm) {

		beanForm.setAllDistricts(ComboBoxInit.getAllDistricts(true));
		beanForm.setAllProvinces(ComboBoxInit.getAllProvinces(true));
	}
	
	private String validateForm (LocationUpdateForm beanForm) {
		
		if(StringUtils.isBlank(CommonUtils.normalizeString(beanForm.getName()))){
			return "Location name cannot be blank!";
		}

		return StringConstants.BLANK;
	}
}
